package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.view.userInfo.entity.VipImgAuthentication;
import com.rongshu.view.userInfo.listener.OnVipImgFunctionClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class VipImgAuthenticationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //list
    private List<VipImgAuthentication> vipImgAuthenticationList = null;
    //content
    private Context context = null;
    //listener
    private OnVipImgFunctionClickListener listener = null;

    public VipImgAuthenticationAdapter(Context context) {
        this.context = context;
    }

    public void setVipImgAuthenticationList(List<VipImgAuthentication> list) {
        this.vipImgAuthenticationList = list;
    }

    /**
     * 根据类型返回数据
     *
     * @param type
     */
    public List<VipImgAuthentication> getVipImgAuthenticationListForType(int type) {
        List<VipImgAuthentication> list = new ArrayList<>();
        if (vipImgAuthenticationList == null) {
            return list;
        }
        for (VipImgAuthentication authentication : vipImgAuthenticationList) {
            if (authentication.getVipImgType() == type) {
                list.add(authentication);
            }
        }
        return list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_vip_img_authentication_view, viewGroup, false);
        return new VipAuthenticationViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        VipImgAuthentication authentication = vipImgAuthenticationList.get(i);
        VipAuthenticationViewHolder holder = (VipAuthenticationViewHolder) viewHolder;
        final int type = authentication.getVipImgType();
        //img
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.default_upload_icon);
        switch (type) {
            case 1:
                Glide.with(context)
                        .load(authentication.getVipImgPath())
                        .apply(options)
                        .into(holder.iv_img);
                break;
        }
        //listener
        holder.iv_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(type, i);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return vipImgAuthenticationList == null ? 0 : vipImgAuthenticationList.size();
    }

    public class VipAuthenticationViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_img)
        ImageView iv_img;

        public VipAuthenticationViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 接口设置
     */
    public void setOnVipImgFunctionClickListener(OnVipImgFunctionClickListener listener) {
        this.listener = listener;
    }
}
